home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / xview / genial / lib / backup / speed_test.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-14  |  5.1 KB  |  213 lines

  1.  
  2. /* speed test program for 3D arrays routines  */
  3. /* Brian Tierney, LBL,  10/90 */
  4.  
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7.  
  8. #define Calloc(x,y) (y *)calloc((unsigned)(x), sizeof(y))
  9.  
  10. #define SX 90
  11. #define SY 100
  12. #define SZ 110
  13.  
  14. /*****************************************************/
  15. main()
  16. {
  17.     int       size, val, xlimit, ylimit, zlimit;
  18.     register int i, j, k, idx1, idx2;
  19.     register u_char   *aptr;        /* pointer to array */
  20.  
  21.     long      tbuf[4], ut1, ut2;/* for recording process times */
  22.  
  23.     /* array type 1: statically allocated 3D array */
  24.     u_char    array1[SX][SY][SZ];
  25.  
  26.     /* array type 2: dynamically allocated 1D array */
  27.     u_char   *array2;
  28.  
  29.     /* array type 3: dynamically allocated 3D array */
  30.     u_char ***array3;
  31.  
  32.     u_char ***alloc_3d_byte_array();
  33.  
  34.     size = SX * SY * SZ;
  35.  
  36.     /* allocate dynamic arrays */
  37.     array3 = alloc_3d_byte_array(SX, SY, SZ);
  38.  
  39.     array2 = (u_char *) calloc(size, sizeof(u_char));
  40.  
  41.     /*****************************************************************/
  42.     /* sequential access test                                        */
  43.     /*****************************************************************/
  44.  
  45.     fprintf(stderr, "\n Sequential access test: \n");
  46.  
  47.     times(tbuf);
  48.     ut1 = tbuf[0];
  49.  
  50.     for (i = 0; i < SX; i++)
  51.     for (j = 0; j < SY; j++)
  52.         for (k = 0; k < SZ; k++)
  53.         array1[i][j][k] = 100;
  54.  
  55.     times(tbuf);
  56.     ut2 = tbuf[0];
  57.     fprintf(stderr, "result method 1: user time was = %d\n", ut2 - ut1);
  58.  
  59.     /*************************************/
  60.  
  61.     times(tbuf);
  62.     ut1 = tbuf[0];
  63.  
  64.     for (i = 0; i < size; i++) {
  65.     array2[i] = 100;
  66.     }
  67.  
  68.     times(tbuf);
  69.     ut2 = tbuf[0];
  70.     fprintf(stderr, "result method 2: user time was = %d\n", ut2 - ut1);
  71.  
  72.     /*************************************/
  73.  
  74.     times(tbuf);
  75.     ut1 = tbuf[0];
  76.  
  77.     aptr = array2;
  78.  
  79.     for (i = 0; i < size; i++) {
  80.     *(aptr++) = 100;
  81.     }
  82.  
  83.     times(tbuf);
  84.     ut2 = tbuf[0];
  85.     fprintf(stderr, "result method 3: user time was = %d\n", ut2 - ut1);
  86.  
  87.     /*************************************/
  88.  
  89.     times(tbuf);
  90.     ut1 = tbuf[0];
  91.  
  92.     for (i = 0; i < SX; i++)
  93.     for (j = 0; j < SY; j++)
  94.         for (k = 0; k < SZ; k++)
  95.         array3[i][j][k] = 100;
  96.  
  97.     times(tbuf);
  98.     ut2 = tbuf[0];
  99.     fprintf(stderr, "result method 4: user time was = %d\n", ut2 - ut1);
  100.  
  101.  
  102.     /*****************************************************************/
  103.     /* random     access test                                        */
  104.     /*****************************************************************/
  105.  
  106.     fprintf(stderr, "\n Random access test: \n");
  107.  
  108.     xlimit = SX - 1;
  109.     ylimit = SY - 1;
  110.     zlimit = SZ - 1;
  111.  
  112.     times(tbuf);
  113.     ut1 = tbuf[0];
  114.  
  115.     for (i = 1; i < xlimit; i++)
  116.     for (j = 1; j < ylimit; j++)
  117.         for (k = 1; k < zlimit; k++)
  118.         val = array1[i - 1][j - 1][k + 1] + array1[i + 1][j + 1][k - 1];
  119.  
  120.     times(tbuf);
  121.     ut2 = tbuf[0];
  122.     fprintf(stderr, "result method 1: user time was = %d\n", ut2 - ut1);
  123.  
  124.     /*************************************/
  125.  
  126.     times(tbuf);
  127.     ut1 = tbuf[0];
  128.  
  129.     for (i = 1; i < xlimit; i++)
  130.     for (j = 1; j < ylimit; j++)
  131.         for (k = 1; k < zlimit; k++) {
  132.         idx1 = ((i - 1) * SX) + ((j - 1) * SY) + (k + 1);
  133.         idx2 = ((i + 1) * SX) + ((j + 1) * SY) + (k - 1);
  134.         val = array2[idx1] + array2[idx2];
  135.         }
  136.  
  137.     times(tbuf);
  138.     ut2 = tbuf[0];
  139.     fprintf(stderr, "result method 2: user time was = %d\n", ut2 - ut1);
  140.  
  141.     /*************************************/
  142.  
  143.     times(tbuf);
  144.     ut1 = tbuf[0];
  145.  
  146.     aptr = array2;
  147.  
  148.     for (i = 1; i < xlimit; i++)
  149.     for (j = 1; j < ylimit; j++)
  150.         for (k = 1; k < zlimit; k++) {
  151.         idx1 = ((i - 1) * SX) + ((j - 1) * SY) + (k + 1);
  152.         idx2 = ((i + 1) * SX) + ((j + 1) * SY) + (k - 1);
  153.         val = *(aptr + idx1) + *(aptr + idx2);
  154.         }
  155.  
  156.  
  157.     times(tbuf);
  158.     ut2 = tbuf[0];
  159.     fprintf(stderr, "result method 3: user time was = %d\n", ut2 - ut1);
  160.  
  161.     /*************************************/
  162.  
  163.     times(tbuf);
  164.     ut1 = tbuf[0];
  165.  
  166.     for (i = 1; i < xlimit; i++)
  167.     for (j = 1; j < ylimit; j++)
  168.         for (k = 1; k < zlimit; k++)
  169.         val = array3[i - 1][j - 1][k + 1] +
  170.             array3[i + 1][j + 1][k - 1];
  171.  
  172.     times(tbuf);
  173.     ut2 = tbuf[0];
  174.     fprintf(stderr, "result method 4: user time was = %d\n", ut2 - ut1);
  175.  
  176.     /*************************************/
  177.  
  178.     fprintf(stderr, "\n\n finished. \n\n");
  179.     return (0);
  180. }
  181.  
  182. /******************************************************************/
  183. u_char ***
  184. alloc_3d_byte_array(nx, ny, nz)
  185.     int       nx, ny, nz;
  186. {
  187.     u_char ***array;
  188.     register int i, j;
  189.  
  190.     /* allocate 3-d array for input image data */
  191.  
  192.     /* allocate 2 arrays of pointers */
  193.     if ((array = Calloc(nx, u_char **)) == NULL)
  194.     perror("calloc error: array ");
  195.     if ((array[0] = Calloc(nx * ny, u_char *)) == NULL)
  196.     perror("calloc error: array ");
  197.  
  198.     /* allocate array for data */
  199.     if ((array[0][0] = Calloc(nx * ny * nz, u_char)) == NULL)
  200.     perror("calloc error: array ");
  201.  
  202.     /* initialize pointer arrays */
  203.     for (i = 1; i < ny; i++)
  204.     array[0][i] = **array + (nz * i);
  205.     for (i = 1; i < nx; i++) {
  206.     array[i] = *array + (ny * i);
  207.     array[i][0] = **array + (nz * ny * i);
  208.     for (j = 1; j < ny; j++)/* initialize pointer array */
  209.         array[i][j] = array[i][0] + (nz * j);
  210.     }
  211.     return (array);
  212. }
  213.